﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Path.Question;

namespace Path.Answer
{
    public class Libby : IPath
    {
        public string GetRelativePath(string currentPath, string targetPath)
        {
            StringBuilder command = new StringBuilder();
            string[] currPath = currentPath.Split('/');
            string[] targPath = targetPath.Split('/');


            if (currPath[1] == targPath[1])
            {
                int i = 1;

                while (i < currPath.Length && i < targPath.Length &&
                    currPath[i] == targPath[i])
                { i++; }
                i--;

                if (i == currPath.Length - 1)
                {
                    command.Append(targetPath.Substring(currentPath.Length + 1));
                }
                else if (i == targPath.Length - 1)
                {
                    for (int j = i + 1; j < currPath.Length; j++)
                    {
                        command.Append("../");
                    }
                    command.Remove(command.Length - 1, 1);
                }
                else
                {
                    for (int j = i + 1; j < currPath.Length; j++)
                    {
                        command.Append("../");
                    }

                    for (int j = i + 1; j < targPath.Length; j++)
                    {
                        command.Append(targPath[j]);
                        if (j != targPath.Length - 1)
                        {
                            command.Append("/");
                        }
                    }
                }
            }
            else
            {
                if (currPath[1] != "")
                {
                    for (int i = 1; i < currPath.Length; i++)
                    {
                        command.Append("../");
                    }

                }

                if (targPath[1] != "")
                {
                    command.Append(targetPath.Substring(1,targetPath.Length-1));
                }
                else if(targPath.Length>1)
                {
                    command.Remove(command.Length - 1, 1);
                }

            }
            return command.ToString();
        }
    }
}
